//
//  AlertViewController.h
//  Riji
//
//  Created by liujianlong on 2022/8/18.
//

#import "NSObject+ext.h"

@implementation NSObject (ext)
- (UIViewController *)currentVC
{
    UIViewController *result = nil;
    UIWindow *window = [self getkeyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow *tpmwin in windows)
        {
            if (tpmwin.windowLevel == UIWindowLevelNormal)
            {
                window = tpmwin;
                break;
            }
        }
    }
    result = window.rootViewController;
    while (result.presentedViewController)
    {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[UITabBarController class]])
    {
        result = [(UITabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]])
    {
        result = [(UINavigationController *)result visibleViewController];
    }
    if ([result isKindOfClass:[AlertViewController class]])
    {
        AlertViewController *  s = (AlertViewController *)result;
        result = result.presentingViewController;
    }
    return result;
}
- (UIWindow *)getkeyWindow
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window?[UIApplication sharedApplication].delegate.window:[UIApplication sharedApplication].keyWindow;
    NSAssert(window, @"%@没有找到window",NSStringFromClass([self class]));
    return window;
}
@end
